package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.OrderDao;
import cn.gson.crm.model.dao.OrderDetailsDao;
import cn.gson.crm.model.mapper.OrderMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrderSerivice {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderDao orderDao;
    @Autowired
    OrderDetailsDao orderDetailsDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询负责人下所有合同
    public Map<String,Object> selectorder(Integer cid,String dept,Integer page,Integer size,String jinnian,String qunian,String qiannian){
        List<Order> orders2=orderMapper.selectorder(cid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Order> orders=orderMapper.selectorder(cid);
        //订单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        List<Employee> employeeList=orderMapper.selectLB();
        List<Employee> employeeList2=orderMapper.selectsp(dept);
        List<Contract> contractList=orderMapper.selectcontract(cid);
        Map<String,Object> map = new HashMap<>();
        //统计图时间查询数据
        List<Order> contractList4=orderMapper.selectnian(cid,jinnian);
        List<Order> contractList5=orderMapper.selectnian(cid,qunian);
        List<Order> contractList6=orderMapper.selectnian(cid,qiannian);
        map.put("total",obj.getTotal());
        map.put("rows",orders);
        map.put("order","DD"+newDate+result);
        map.put("jinli",employeeList2);
        map.put("lb",employeeList);
        map.put("contrat",contractList);
        map.put("tong",orders2);
        map.put("jinnian",contractList4);
        map.put("qunian",contractList5);
        map.put("qiannian",contractList6);
        return map;
    };
    //根据合同id查询此合同的报价
    public List<Sales> selectbaojia(Integer cid){
        return orderMapper.selectbaojia(cid);
    };
    //新增订单和详细表
    public void saveorder(HttpSession session,Order order){
       if(order.getOrderId()!=0){
           orderDao.save(order);
           Check check = new Check();
           User login = (User)session.getAttribute("login");
           int yname= (int) login.getEmployees().get(0).getYgId();
           check.setChecksqinName((long) yname);
           check.setCheckType("订单审批");
           check.setChecktypeid((int) order.getOrderId());
           check.setCheckDate(order.getOrderTime());
           check.setCheckName(order.getEreApprover());
           check.setCheckCondition("未审核");
       }else {
           orderDao.save(order);
           order.getOrderDetailsList().forEach(p->{
               p.setOrderByOrderId(order);
           });
           orderDetailsDao.saveAll(order.getOrderDetailsList());
       }
    }
    //条件查询订单状态
    public Map<String,Object> selectstate(HttpSession session, Integer cid, String state,Integer page,Integer size){
        cid=fouyuan(session);
        if(state.equals("全部订单")){
            Page<Object> obj = PageHelper.startPage(page, size);
            List<Order> orders=orderMapper.selectorder(cid);
            Map<String,Object> map = new HashMap<>();
            map.put("total",obj.getTotal());
            map.put("rows",orders);
            return map;
        }else {
            Page<Object> obj = PageHelper.startPage(page, size);
            List<Order> orders = orderMapper.selectstate(cid, state);
            Map<String, Object> map = new HashMap<>();
            map.put("total", obj.getTotal());
            map.put("rows", orders);
            return map;
        }
    };
    //根据合同编号和订单号查询订单
    public Map<String,Object> selectwhere( Integer cid, String ocnumber,Integer page,Integer size){
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Order> orders=orderMapper.selectwhere(cid,ocnumber);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",orders);
        return map;
    };
    //高级查询
    public Map<String,Object>selectgaoji(HttpSession session,Integer cid, String onumber,  String cnumber, String condition6,String ecstate,Integer page,Integer size){
        cid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Order> orders=orderMapper.selectgaoji(cid,onumber,cnumber,condition6,ecstate);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",orders);
        return map;
    };

    //根据id查询订单详情
    public List<OrderDetails> selectoid(Integer oid){
        return orderMapper.selectoid(oid);
    };
    //主页查询订单金额和汇款金额1
    public Map<String,Object> selectOver(String orderTime){
        List<Order> orders=orderMapper.selectOver();
        Map<String ,Object> map=new HashMap<>();
        List<Integer> ma2=new ArrayList<>();//主页订单金额
        List<Integer> ma3=new ArrayList<>();//主页订单回款
        List<Date> ma4=new ArrayList<>();//时间

        for(int i=0;i<orders.size();i++){
            ma2.add(orderDao.selectfive("%"+orderTime+"%"));
            ma3.add(orderDao.selectfour("%"+orderTime+"%"));
            ma4.add(orderMapper.selectOver().get(i).getOrderTime());
        }
        map.put("ma2",ma2);
        map.put("ma3",ma3);
        map.put("ma4",ma4);
        return map;

    }
//    主页查询订单金额和回款金额2
    public Map<String,Object> selectOver2(String orderTime){
        List<Order> orders=orderMapper.selectOver();
        Map<String ,Object> map=new HashMap<>();
        List<Integer> ma2=new ArrayList<>();//主页订单金额
        List<Integer> ma3=new ArrayList<>();//主页订单回款
        List<Date> ma4=new ArrayList<>();//时间

        for(int i=0;i<orders.size();i++){
            ma2.add(orderDao.selectseven("%"+orderTime+"%"));
            ma3.add(orderDao.selectsix("%"+orderTime+"%"));
            ma4.add(orderMapper.selectOver().get(i).getOrderTime());
        }
        map.put("ma2",ma2);
        map.put("ma3",ma3);
        map.put("ma4",ma4);
        return map;
    }
   //查询总订单金额和已付清的金额
    public Map<String,Object> selectMoney(){
        List<Order> list =orderMapper.selectMoney();
        Map<String,Object> map =new HashMap<>();
        List<Integer> m1=new ArrayList<>();
        List<Integer> m2=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            System.out.println(orderDao.selectMoney(list.get(i).getOrderMoney()));
            m1.add(orderDao.selectMoney(list.get(i).getOrderMoney()));
            m2.add(orderDao.selectMoney2(list.get(i).getOrderMoney()));
        }
        map.put("m1",m1);
        map.put("m2",m2);
        return map;
    }
    //查询总回款金额和已付清的回款金额
    public Map<String,Object> selectMoney2(){
        List<Order> list =orderMapper.selectMoney();
        Map<String,Object> map =new HashMap<>();
        List<Integer> m1=new ArrayList<>();
        List<Integer> m2=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            System.out.println(orderDao.selectMoney(list.get(i).getRefundAmont()));
            m1.add(orderDao.selectMoney3(list.get(i).getRefundAmont()));
            m2.add(orderDao.selectMoney4(list.get(i).getRefundAmont()));
        }
        map.put("m1",m1);
        map.put("m2",m2);
        return map;
    }
}
